<?php

require_once 'includes/constants.php';

class Mysql {
	private $conn;
	
	function __construct() {
		$this->conn = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME) or 
					  die('There was a problem connecting to the database.');
	}
	function verify_unused_username($un){
        $query = "SELECT *
				FROM user
				WHERE uname = ?
				LIMIT 1";
		
		if($stmt = $this->conn->prepare($query)) {
			$stmt->bind_param('s', $un);
			$stmt->execute(); 
            $stmt->store_result(); 
                        
            //custom class :D bind to Statement Result mambo jambo! 
            $sr = new Statement_Result($stmt); 
                        
			if($stmt->fetch() ==  NULL) {
				$stmt->close();                
				return true;
			} else {
                return false;
            }
		}
    
    }
    function register_user($un,$pwd,$email){
        $pwd = md5($pwd);
        return $this->conn->query("INSERT INTO user (uname,password,email) VALUES('$un','$pwd','$email')");
    }
	
	function set_pwd($un,$pwd){
        $pwd = md5($pwd);
        return $this->conn->query("UPDATE user SET password = '$pwd' WHERE uname = '$un'");
    }
    
    // submit the information of user
    function submit_user_data($userID,$fname,$lname,$mi,$address,$dob,$phone,$email){
    	return $this->conn->query("UPDATE user SET fname = '$fname', lname = '$lname', mi = '$mi', address = '$address', dob = '$dob', phone ='$phone',email ='$email' WHERE id = '$userID'");
    }
    
    
    
    function run_query(){
		if(func_num_args()<1){
			return;
		}
		$stmt = $this->conn->stmt_init();
		$stmt = $this->conn->prepare(func_get_arg(0));
		
		switch(func_num_args()){
			case 0:
				return;
				break;
			case 1:
				break;
			case 2:
				$stmt->bind_param('s', func_get_arg(1));
				break;
			case 3:
				$stmt->bind_param('ss', func_get_arg(1),func_get_arg(2));
				break;
			case 4:
				$stmt->bind_param('sss', func_get_arg(1),func_get_arg(2),func_get_arg(3));
				break;
			case 5:
				$stmt->bind_param('ssss', func_get_arg(1),func_get_arg(2),func_get_arg(3),func_get_arg(4));
				break;
			case 6:
				$stmt->bind_param('sssss', func_get_arg(1),func_get_arg(2),func_get_arg(3),func_get_arg(4),func_get_arg(5));
				break;
			case 7:
				$stmt->bind_param('ssssss', func_get_arg(1),func_get_arg(2),func_get_arg(3),func_get_arg(4),func_get_arg(5),func_get_arg(6));
				break;
			case 8:
				$stmt->bind_param('sssssss', func_get_arg(1),func_get_arg(2),func_get_arg(3),func_get_arg(4),func_get_arg(5),func_get_arg(6),func_get_arg(7));
				break;
			case 9:
				$stmt->bind_param('ssssssss', func_get_arg(1),func_get_arg(2),func_get_arg(3),func_get_arg(4),func_get_arg(5),func_get_arg(6),func_get_arg(7),func_get_arg(8));
				break;
			case 10:
				$stmt->bind_param('sssssssss', func_get_arg(1),func_get_arg(2),func_get_arg(3),func_get_arg(4),func_get_arg(5),func_get_arg(6),func_get_arg(7),func_get_arg(8),func_get_arg(9));
				break;
				
		}
		$stmt->execute();
		return $stmt;
	}
    function run_query_string($query_string){        
        return $this->conn->query($query_string);
    }
    
	function verify_Username_and_Pass($un, $pwd) {
				
		$query = "SELECT *
				FROM user
				WHERE uname = ? AND password = ?
				LIMIT 1";
		
		if($stmt = $this->conn->prepare($query)) {
			$stmt->bind_param('ss', $un, md5($pwd));
			$stmt->execute();
			
            $stmt->store_result(); 
                        
            //custom class :D bind to Statement Result mambo jambo! 
            $sr = new Statement_Result($stmt); 
			if($stmt->fetch()) {
				$stmt->close();
                //Username and Pw valid
                //Get UserID
                //setcookie(session_name(),$sessionid,$time+3600,'/','');
                //$this->conn->query("DELETE FROM user_sessions WHERE user_id='$userid'");
                //$this->conn->query("INSERT INTO user_sessions (session_id,user_id,timestamp) VALUES('$sessionid','$userid','$time')");
				$_SESSION['userid'] = $sr->Get('id');
                $_SESSION['username'] = $sr->Get('uname');
				return true;
			} else {
				return false;
			}
		}
		
	}
    
    
    function addmedia_addmovie(){
    
    
    }
    
    
    
}


class Statement_Result 
{ 
    private $_bindVarsArray = array(); 
    private $_results = array(); 

    public function __construct(&$stmt) 
    { 
        $meta = $stmt->result_metadata(); 

        while ($columnName = $meta->fetch_field()) 
            $this->_bindVarsArray[] = &$this->_results[$columnName->name]; 

        call_user_func_array(array($stmt, 'bind_result'), $this->_bindVarsArray); 
        
        $meta->close(); 
    } 
    
    public function Get_Array() 
    { 
        return $this->_results;    
    } 
    
    public function Get($column_name) 
    { 
        return $this->_results[$column_name]; 
    } 
} 